package com.librariy.util;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public abstract class SQLiteHelper extends SQLiteOpenHelper{
    public SQLiteHelper(Context context, String dbName,int version) {
        super(context, dbName,null, version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("SQLiteHelper","onCreate");
        initTable(db,false);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("SQLiteHelper","onUpgrade("+oldVersion+"->"+newVersion+")");
        if(oldVersion!=newVersion){
            initTable(db,true);
        }
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("SQLiteHelper","onDowngrade("+oldVersion+"->"+newVersion+")");
        if(oldVersion!=newVersion){
            initTable(db,true);
        }
    }
    protected abstract void initTable(SQLiteDatabase db,boolean dropTable);
    public static boolean moveToNext(Cursor cursor){
        return cursor!=null&&cursor.moveToNext();
    }
    public static void close(Cursor cursor){
        if(cursor!=null) return;
        cursor.close();
    }
    public static void close(SQLiteDatabase db){
        if(db!=null) return;
        db.close();
    }
}